package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hp.bon.logreverse.ExtractHelper;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.util.Comutils;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class BlackCardQueryBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(BlackCardQueryBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {
            String accNbr = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/Acc_Nbr");
            String startTime = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/StartTime");
            String endTime = ExtractHelper.getXmlNodeValue(xmlMessage.getDataElement(), "/Service_Information/EndTime");

            List<Element> elements = xmlMessage.getDataElement().selectNodes("/Service_Information/Bill_Information");
            List<String> list = new ArrayList<>();
            for (Element item : elements) {
                list.add(item.getStringValue());
            }

            //构建请求条件
            StringBuffer msgBodyStr = new StringBuffer();
            jsonToXmlStr(bizParam, msgBodyStr, accNbr, list, startTime, endTime);
            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            String respStr = HttpClientUtils.postHttpStr(msgBodyStr.toString(), webserviceUrl);

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            bizResult.setHttpBody(respStr);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(BizParam bizParam, StringBuffer buffer, String accNbr, List<String> list, String startTime, String endTime) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<HqCheck>");
        buffer.append("<BillingMode>1</ BillingMode >");
        buffer.append("<MDN>" + accNbr + "</MDN>");
        buffer.append("<ServId>" + bizParam.getParamString("prodInstId") + "</ServId>");
        buffer.append("<AreaCode>" + bizParam.getParamString("regionId") + "</AreaCode>");
        buffer.append("<CkMDNlist>");
        for (int i = 0; i < list.size(); i++) {
            buffer.append("<CkMDN>" + list.get(i) + "</CkMDN>");
        }
        buffer.append("</CkMDNlist>");
        buffer.append("<StartTime>" + startTime + "</StartTime>");
        buffer.append("<EndTime>" + endTime + "</EndTime>");
        buffer.append("</HqCheck>");
    }

}
